<?php
//by ajsong 172403414@qq.com 2015/06/29

class other extends core {
	public function __construct() {
		parent::__construct();
	}

	//配置开关
	//on1216: 12月16日提交给苹果的审核参数
	//WX_APPID WX_MCHID WX_APPSECRET WX_PARTNERID
	public function c() {
		$configs = array(
			"on0308"=>0
		);
		success($configs);
	}
	
	//RSA证书生成
	public function rsa() {
		global $table_prefix;
		new Rsa(FRAMEWORK_PATH . '/class/rsa/keys', "{$table_prefix}private", "{$table_prefix}public");
		exit('Certificate created success');
	}

	public function map() {
		$longitude = (isset($_GET['longitude']) && trim($_GET['longitude'])) ? floatval($_GET['longitude']) : 0;
		$latitude = (isset($_GET['latitude']) && trim($_GET['latitude'])) ? floatval($_GET['latitude']) : 0;
		success('ok');
	}

	public function handle_meal() {
		$meal_ids = $op_ids = "";
		$sql = "select m.id as mid, o.id as order_id, p.id as opid, o.add_time 
			from jf_member_meal m INNER JOIN im_order o ON m.member_id=o.member_id  
			INNER JOIN jf_order_project p ON o.id=p.order_id 
			where m.meal_id=10 and m.num=1 and o.total_price=0 and o.status>=4 
			and p.project_id=22 AND p.meal_id=0";
		$meals = $this->db->get_results($sql);
		if ($meals) {
			foreach ($meals as $key => $row) {
				if ($meal_ids) {
					$meal_ids .= ", {$row->mid}";
				} else {
					$meal_ids = $row->mid;
				}
				if ($op_ids) {
					$op_ids .= ", {$row->opid}";
				} else {
					$op_ids = $row->opid;
				}
			}
			$sql = "UPDATE jf_order_project SET meal_id='10' WHERE id IN($op_ids) AND meal_id=0";
			echo $sql;
			$this->db->query($sql);
			//exit;
			$sql = "UPDATE jf_member_meal SET num=0 WHERE id IN ({$meal_ids})";
			echo $sql;
			$this->db->query($sql);
		}
	}

	public function project_ids() {
		$rs = $this->db->get_results("select id from jf_order");
		foreach ($rs as $order) {
			$projects = $this->db->get_results("select project_id from jf_order_project where order_id='{$order->id}'");
			if ($projects) {
				$ids = "";
				foreach ($projects as $k => $p) {
					$ids .= $p->project_id.",";
				}
				if ($ids) $ids = ",{$ids}";
				$sql = "update jf_order set project_ids='{$ids}' where id='{$order->id}'";
				echo $sql."<br>";
				$this->db->query($sql);
			}
		}
	}

	public function info() {
		//phpinfo();
	}
	
	//物流查询
	//快递鸟接口
	public function kdniao() {
		$spell_name = (isset($_GET['spell_name']) && trim($_GET['spell_name'])) ? trim($_GET['spell_name']) : '';
		$mail_no = (isset($_GET['mail_no']) && trim($_GET['mail_no'])) ? trim($_GET['mail_no']) : '';
		if (!$spell_name || !$mail_no) error('数据错误');
		$result = getkdniao($spell_name, $mail_no);
		success($result);
	}
	//爱查快递接口
	public function ickd() {
		$spell_name = (isset($_GET['spell_name']) && trim($_GET['spell_name'])) ? trim($_GET['spell_name']) : '';
		$mail_no = (isset($_GET['mail_no']) && trim($_GET['mail_no'])) ? trim($_GET['mail_no']) : '';
		if (!$spell_name || !$mail_no) error('数据错误');
		$result = getickd($spell_name, $mail_no);
		success($result);
	}

	//给所有旧用户都发一张68元
	public function send_coupon() {
		$sql = "SELECT * FROM jf_member";
		$members = $this->db->get_results($sql);
		foreach ($members as $k => $member) {
			$sql = "SELECT count(*) FROM jf_member_cash_coupon WHERE member_id='{$member->id}' AND cash_coupon_id=1";
			if (intval($this->db->get_var($sql))==0) {
				$sql = "INSERT INTO jf_member_cash_coupon(member_id, cash_coupon_id, cash_coupon_name, face_value, 
					expiring_time, add_time, status, used_time) 
					VALUES('{$member->id}', '1', '现金券', '68', '1454255999', '1451544112', '1', '0')";
				echo $sql."<br>";
				$this->db->query($sql);
			}
		}
	}

	public function import_member() {
		require_once FRAMEWORK_PATH . '/class/PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
		$objPHPExcel = PHPExcel_IOFactory::load(ROOT_PATH."/temp/member.xlsx");
		//echo date('H:i:s') ;
		//$objWorksheet = $objPHPExcel->getActiveSheet();
		 $sheets = $objPHPExcel->setActiveSheetIndex(0);
		 foreach ($objPHPExcel->getWorksheetIterator() as $objWorksheet) {
			//获取行数
			$highestRow = $objWorksheet->getHighestRow();
			//获取列数
			$highestColumn = $objWorksheet->getHighestColumn();
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
			$data = array();
			for($row = 1; $row <= $highestRow; ++$row) {
				for($col = 0; $col < $highestColumnIndex; ++$col) {
					$val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
					$data[$row][$col] = trim($val);
				}
			}

			foreach ($data as $k => $row) {
				if ($k>=2) {
					//写入会员
					$fenyuan = trim($row[0]);
					$kahao = trim($row[1]);
					$name = trim($row[2]);
					$tel = trim($row[3]);
					$mobile = trim($row[4]);
					if ($tel=="" && $mobile=="") {
						echo "<p style='color:red'>{$fenyuan} {$kahao} {$name}电话为空<br></p>";
					} else {
						if ($mobile=="") {
							$mobile = $tel;
						}
						if ($mobile && is_numeric($mobile)) {
							$sql = "SELECT count(*) FROM jf_member WHERE mobile='{$mobile}'";
							if ($this->db->get_var($sql)) {
								echo "<p style='color:red'>{$fenyuan} {$kahao} {$name}电话已经存在<br></p>";
							} else {
								$sql = "INSERT INTO jf_member(name, mobile, reg_time, nick_name, fenyuan, kahao) 
									VALUES('{$name}', '{$mobile}', '".time()."', '{$name}', '{$fenyuan}', '{$kahao}')";
								$this->db->query($sql);
								echo $sql."<br>";
							}
						} else {
							echo "<p style='color:red'>{$fenyuan} {$kahao} {$name}电话为空<br></p>";
						}
						//exit;
					}
				}
			}
		}
	}

	public function import_jiameng() {
		require_once FRAMEWORK_PATH . '/class/PHPExcel_1.8.0/Classes/PHPExcel/IOFactory.php';
		$objPHPExcel = PHPExcel_IOFactory::load(ROOT_PATH."/temp/jiameng.xlsx");
		//echo date('H:i:s') ;
		//$objWorksheet = $objPHPExcel->getActiveSheet();
		 $sheets = $objPHPExcel->setActiveSheetIndex(0);
		 foreach ($objPHPExcel->getWorksheetIterator() as $objWorksheet) {
			//获取行数
			$highestRow = $objWorksheet->getHighestRow();
			//获取列数
			$highestColumn = $objWorksheet->getHighestColumn();
			$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
			$data = array();
			for($row = 1; $row <= $highestRow; ++$row) {
				for($col = 0; $col < $highestColumnIndex; ++$col) {
					$val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
					$data[$row][$col] = trim($val);
				}
			}

			foreach ($data as $k => $row) {
				if ($k>=1) {
					//写入会员
					$fenyuan = trim($row[2]);
					$name = trim($row[0]);
					$mobile = trim($row[1]);
					if ($mobile=="") {
						echo "<p style='color:red'>{$fenyuan} {$name}电话为空<br></p>";
					} else {
						if ($mobile && is_numeric($mobile)) {
							$sql = "SELECT count(*) FROM jf_member WHERE mobile='{$mobile}'";
							if ($this->db->get_var($sql)) {
								$sql = "UPDATE jf_member SET member_type='3' WHERE mobile='{$mobile}'";
								$this->db->query($sql);
								echo "<p style='color:red'>{$fenyuan} {$name}电话已经存在<br></p>";
							} else {
								$sql = "INSERT INTO jf_member(name, mobile, reg_time, nick_name, fenyuan) 
									VALUES('{$name}', '{$mobile}', '".time()."', '{$name}', '{$fenyuan}')";
								$this->db->query($sql);
								echo $sql."<br>";
							}
						} else {
							echo "<p style='color:red'>{$fenyuan} {$name}电话为空<br></p>";
						}
						//exit;
					}
				}
			}
		}
	}

	public function add_activity() {
		echo uniqid();
		exit;
		$sql = "SELECT * FROM jf_activity_zhuanpan WHERE id='8'";
		$activity = $this->db->get_row($sql);
		for ($id=49; $id<=61; $id++) {
			$sql = "INSERT INTO jf_activity_zhuanpan_jiangpin(activity_id, name, description, pic, 
				quantity, jiangpin_type, ext_id, people, open_time) 
				VALUES('{$id}', '{$activity->name}', '{$activity->description}', '{$activity->pic}', 
				'{$activity->quantity}', '{$activity->jiangpin_type}', '{$activity->ext_id}', '{$activity->people}', '{$activity->open_time}')";
			echo $sql."<br>";
			$this->db->query($sql);
		}
	}
	
	//android检测更新,在后台参数设置处增加 g_update_version、g_update_content、g_update_url
	public function update() {
		$data = null;
		if (isset($this->configs['g_update_version'])) {
			$data = new stdClass();
			$data->version = $this->configs['g_update_version'];
			$data->content = $this->configs['g_update_content'];
			$data->url = $this->configs['g_update_url'];
		}
		success($data);
	}

	public function jspatch() {
		$html = '';
		/*//
		$html = "require('member,UIImage')
		defineClass('member', {
			loadViews:function() {
				self.ORIGloadViews();
				var avatar = self.valueForKey('_avatar');
				avatar.setImage(UIImage.imageNamed('m-logo'));
			}
		})";
		//*/
		exit($html);
	}

}
